<div id="result">
<form method="post" id="import_global_brand" action="<?php echo Configure::read('base_url')."add_category/importglobals";?>">
<?php echo $this->Form->input('Category.id', array( 'type' => 'hidden', 'value' => $this->data['id'] ));?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="mytable">
<?php if(isset($this->data['global_brand'])):?>
  <tr><td colspan="2"><h5>Import Global Brands</h5></td></tr>
  <tr>
    <td width="30"><input type="checkbox" id="select_brand_all"></td>
    <td width="97%">Select all</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0" class="import_global_brand">
	<?php $i=0;foreach($this->data['global_brand'] as $brand):
		$class = null;
		if ($i++ % 2 == 0)
		$class = ' class="alt"';
		
	?>
      <tr>
        <td width="30" <?php echo $class;?>><input type="checkbox" name="data[global_brand][]" id="global_brand" value="<?php echo $brand['Brand']['id'];?>" /></td>
        <td <?php echo $class;?>><?php echo $brand['Brand']['brand_description'];?></td>
      </tr>
    <?php endforeach;?>  
    </table></td>
  </tr>
  <?php endif;?>
  
  <?php if(isset($this->data['global_style'])):?>
  <tr><td colspan="2"><h5>Import Global Style</h5></td></tr>
  <tr>
    <td width="30"><input type="checkbox" id="select_style_all"></td>
    <td width="97%">Select all</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0" class="import_global_style">
	<?php $i=0;foreach($this->data['global_style'] as $brand):
		$class = null;
		if ($i++ % 2 == 0) 
		$class = ' class="alt"';
	?>
      <tr>
        <td width="30" <?php echo $class;?>><input type="checkbox" name="data[global_style][]" id="global_style" value="<?php echo $brand['Brand']['id'];?>" /></td>
        <td <?php echo $class;?>><?php echo $brand['Brand']['brand_description'];?></td>
      </tr>
    <?php endforeach;?>  
    </table></td>
  </tr>
  <?php endif;?>
  
  <?php if(isset($this->data['global_condition'])):?>
  <tr><td colspan="2"><h5>Import Global Condition</h5></td></tr>
  <tr>
    <td width="30"><input type="checkbox" id="select_condition_all"></td>
    <td width="97%">Select all</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0" class="import_global_condition">
	<?php $i=0;foreach($this->data['global_condition'] as $brand):
		$class = null;
		if ($i++ % 2 == 0)
		$class = ' class="alt"';
	?>
      <tr>
        <td width="30" <?php echo $class;?>><input type="checkbox" name="data[global_condition][]" id="global_condition" value="<?php echo $brand['Brand']['id'];?>" /></td>
        <td <?php echo $class;?>><?php echo $brand['Brand']['brand_description'];?></td>
      </tr>
    <?php endforeach;?>  
    </table></td>
  </tr>
  <?php endif;?>
  <tr>
    <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="75"><input type="submit" name="button" id="button" value="Import" /></td>
        <td><input type="submit" name="button2" id="button2" value="Skip This!" /></td>
      </tr>
    </table></td>
    </tr>
</table>

</form>
</div>
<?php  echo $html->script(array("jquery.min.js","jquery.form.js","jquery.validate.js","jquery.pagination.js","jquery.tablesorter.min.js","jquery.tablednd.js")); ?>	 
 <script type="text/javascript">  
 jQuery(document).ready(function(){
     
 jQuery("#category_table").tablesorter({headers: { 2:{sorter: false},3:{sorter: false}, 4:{sorter: false}, 5:{sorter: false}}}); 
 
		var v = jQuery("#import_global_brand").validate({
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: "#result"
				});
			}
		});		
		
//Check all
		$("#select_brand_all").click(function()				
			{
				var checked_status = this.checked;
				$("input[id=global_brand]").each(function()
				{
					this.checked = checked_status;
				});
			});
			$("#select_style_all").click(function()				
			{
				var checked_status = this.checked;
				$("input[id=global_style]").each(function()
				{
					this.checked = checked_status;
				});
			});
			$("#select_condition_all").click(function()				
			{
				var checked_status = this.checked;
				$("input[id=global_condition]").each(function()
				{
					this.checked = checked_status;
				});
			});


	});
        
var brands=jQuery(".import_global_brand");
var styles=jQuery(".import_global_style");
var condition=jQuery(".import_global_condition");
var brandsCount=1;
var styleCount=1;
var condtionCount=1;


function test(selector){
    selector.find("tr").show();
    className=selector.attr("class");
    if(className=="import_global_brand"){
        count=brandsCount;
        brandsCount++;
    }
    else if(className=="import_global_style"){
        count=styleCount;
        styleCount++;
    }
    else{
        count=condtionCount;
        condtionCount++;
    }
    selector.find("tr").each(function(index){
        if(index > count*5 && index < selector.find("tr").length-1){
            jQuery(this).hide();
        }

    });
    
    menuHide(selector);

}

function lessItem(selector){
    className=selector.attr("class");
    if(className=="import_global_brand"){
        count=brandsCount;
        brandsCount=brandsCount-2;
    }
    else if(className=="import_global_style"){
        count=styleCount;
        styleCount=styleCount-2;
    }
    else{
        count=condtionCount;
        condtionCount=condtionCount-2;
    }

    test(selector);
    menuHide(selector);
}

function menuHide(selector){
    length=parseInt(selector.find("tr").length);
    hiddenFields=parseInt(selector.find("tr:hidden").length);
    
    diff=parseInt(length)-hiddenFields; 
    if(diff <= 7){
        selector.find(".less").hide();
        selector.find(".more").show();
        selector.find(".pipe").hide();
    }

    if(hiddenFields==0){
        selector.find(".less").show();
        selector.find(".more").hide();
        selector.find(".pipe").hide();
    }

    if(diff > 7 && hiddenFields > 0){
        selector.find(".less").show();
        selector.find(".more").show();
        selector.find(".pipe").show();
    }
}

jQuery(".import_global_brand").append('<tr><td></td><td style="text-align: right;"><a onclick="lessItem(brands)" class="less"><< Less</a><span class="pipe"> || </span> <a onclick="test(brands)" class="more">More >></a></td></tr>');
jQuery(".import_global_style").append('<tr><td></td><td style="text-align: right;"><a onclick="lessItem(styles)" class="less"><< Less</a><span class="pipe"> || </span> <a onclick="test(styles)" class="more">More >></a></td></tr>');
jQuery(".import_global_condition").append('<tr><td></td><td style="text-align: right;"><a onclick="lessItem(condition)" class="less"><< Less</a><span class="pipe"> || </span> <a onclick="test(condition)" class="more">More >></a></td></tr>');

test(brands);
test(styles);
test(condition);
</script>

<style type="text/css">
.more,.less{cursor: pointer;}
</style>